//
//  ProgressTableViewCell.swift
//  HuaYun
//
//  Created by enghou on 2018/11/21.
//  Copyright © 2018 xyxorigation. All rights reserved.
//

import UIKit

class ProgressTableViewCell: UITableViewCell {

    var _model: JSON?
    var model: JSON?{
        set{
            _model = newValue
            progressBarView.percent = 0
            typeNameLabel.text = _model?.contents?["type"]?.rawString()
            if let sumplan = _model?.contents?["plan"]?.int {
                let sumpland = sumplan / 1000000
                planValueLabel.text = String(sumpland).rmbformat
                if let sumhappen = _model?.contents?["execute"]?.int {
                    let sumhappend = sumhappen / 1000000
                    executeLabel.text = String(sumhappend).rmbformat
                    if sumpland > 0 {
                        let percent = Double(sumhappend) / Double(sumpland)
                        self.percentLabel.text = String(lround(percent * 100)) + "%"
                        self.progressBarView.percent = percent
                        if percent <= 0.3 {
                            self.percentLabel.textColor = UIColor.hex("eb2439")
                        } else if percent <= 0.6{
                            self.percentLabel.textColor = UIColor.hex("ff9b00")
                        } else if percent <= 0.8 {
                            self.percentLabel.textColor = UIColor.hex("0087ff")
                        } else {
                            self.percentLabel.textColor = UIColor.hex("19b960")
                        }
                    } else {
                        self.progressBarView.percent = 0
                        self.percentLabel.text = "0"
                        self.percentLabel.textColor = UIColor.black
                    }
                }
            }
        }
        get{
            return _model
        }
        
    }
    @IBOutlet weak var typeNameLabel: UILabel!
    @IBOutlet weak var planValueLabel: UILabel!
    @IBOutlet weak var executeLabel: UILabel!
    @IBOutlet weak var percentLabel: UILabel!
    @IBOutlet weak var progressBarView: ProgressView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        progressBarView.backgroundColor = UIColor.hex("eeeeee")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
